package etools

import (
	"strconv"
	"time"
)

var TimeLayout = "2006-01-02 15:04:05"

func TimeNowMS() int64 {
	return time.Now().UnixNano() / 1e6
}

//@title 获取时间
//@author lianpenglin
//@return string类型
func TimeNowString() string {
	return time.Now().Format(TimeLayout)
}

//@title 获取当前带毫秒的时间
//@author lianpenglin
//@return string类型
func TimeNowNanoString() string {
	return time.Now().Format("2006-01-02 15:04:05.000")
}

//@title 获取时间
//@author lianpenglin
//@return Time类型
func DateTimeNow(timeString string) (time.Time, error) {
	local, _ := time.LoadLocation("Local")
	if len(timeString) > 0 {
		return time.ParseInLocation(TimeLayout, timeString, local)
	} else {
		return time.ParseInLocation(TimeLayout, TimeNowString(), local)
	}
}

//@title 时间戳转时间格式
//@author lianpenglin
//@return string类型
func TimeToString(timestamp int64) string {
	return time.Unix(timestamp, 0).Format(TimeLayout)
}

//@title 时间格式转时间戳
//@author lianpenglin
//@return int类型
func StringToTime(timeString string) int64 {
	local, _ := time.LoadLocation("Local")
	location, err := time.ParseInLocation(TimeLayout, timeString, local)
	if err != nil {
		return 0
	}
	return location.Unix()
}

func SecondTimestamp(ts int64) int64 {
	l := len(strconv.FormatInt(ts, 10))
	for l > 10 {
		ts = ts / 10
		l--
	}

	return ts
}
